const xRemote = () => {
	let debug = false;
	let monitor = null;

	const getMonitor = (m) => {
		monitor = m
	}

	const onChangeEvent = (code) => {
		if (debug)
			uni.showToast({
				title: code,
				icon: 'none'
			});
		if (monitor != null) {
			monitor(code)
		}
	}

	const keyListener = (e) => {
		let {
			keyCode
		} = e;
		onChangeEvent(keyCode)
	}

	const backListener = (e) => {
		onChangeEvent("back")
	}

	const removeListener = () => {
		plus.key.removeEventListener("keydown", keyListener);
		plus.key.removeEventListener('backbutton', backListener);
	}

	const addListener = () => {
		plus.key.addEventListener("keydown", keyListener, true);
		plus.key.addEventListener('backbutton', backListener, true)
	}

	const openDebug = () => {
		debug = true;
	}

	const closeDebug = () => {
		debug = false
	}

	return {
		addListener,
		removeListener,
		openDebug,
		closeDebug,
		getMonitor
	}
}
export default xRemote();